﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Dropit.BlogPageProvider.Blog;

namespace Dropit.BlogPageProvider.Blogger
{
	public class Post : IPost
	{
		public static List<Post> List(Service service, BloggerId id)
		{
			return Utility.GetFeedEntries(service, GetFeedUrl(id)).Select<AtomEntry, Post>(entry => new Post()
							{
								BloggerId = new BloggerId()
								{
									BlogId = id.BlogId,
									PostId = entry.Id.Uri.ToString()
								},
								Entry = entry,
								Heading = entry.Title.Text,
								Content = entry.Content.Content,
								Created = entry.Published,
								Category = entry.Categories.Select<AtomCategory, string>(category => category.Term).ToList()
							})
							.ToList<Post>();
		}

		public static Post Load(Service service, BloggerId id)
		{
			return List(service, id).Find(post => post.BloggerId.PostId == id.PostId);
		}

		private static string GetFeedUrl(BloggerId id)
		{
			return string.Format("http://www.blogger.com/feeds/{0}/posts/default", Utility.ExtractBlogId(id.BlogId));
		}

		public static void Update(Service service, BloggerId id, IPost post)
		{
			Post oldPost = Post.Load(service, id);
			if (oldPost != null && oldPost.Entry!=null)
			{
				AtomEntry entry = oldPost.Entry;
				entry.Title.Text = post.Heading;
				if (entry.Content == null)
				{
					entry.Content = new AtomContent();
				}
				entry.Content.Content = post.Content;

				foreach(string category in post.Category)
				{
					entry.Categories.Add(new AtomCategory(category, "http://www.blogger.com/atom/ns#"));
				}
				entry.Update();
			}
		}

		public static Post SaveNew(Service service, BloggerId parentId, IPost post)
		{
			AtomEntry newPost = new AtomEntry();
			newPost.Title.Text = post.Heading;
			newPost.Content = new AtomContent();
			newPost.Content.Content = post.Content;
			post.Category.ForEach(category => newPost.Categories.Add(new AtomCategory(category,"http://www.blogger.com/atom/ns#")));
			newPost.Content.Type = "xhtml";

			Uri blogFeedUri = new Uri(GetFeedUrl(parentId));
			AtomEntry newEntry = service.Insert(blogFeedUri, newPost);
			return new Post()
								{
									BloggerId = new BloggerId()
									{
										BlogId = parentId.BlogId,
										PostId = newEntry.Id.Uri.ToString()
									},
									Entry = newEntry,
									Heading = newEntry.Title.Text,
									Content = newEntry.Content.Content,
									Created = newEntry.Published
								};
		}

		public static void Delete(Service service, BloggerId id)
		{
			Post post = Post.Load(service, id);
			if (post != null && post.Entry != null)
			{
				post.Entry.Delete();
			}
		}

		public string Id
		{
			get { return BloggerId.ToString(); }
		}

		public BloggerId BloggerId { get; set; }
		public AtomEntry Entry { get; set; }
		public string Heading { get; set; }
		public string Content { get; set; }
		public DateTime Created { get; set; }
		public List<string> Category { get; set; }
	}
}
